package com.example.questionnaire.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.questionnaire.dao.*;
import com.example.questionnaire.pojo.DO.*;
import com.example.questionnaire.service.QuestionnaireSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.codehaus.jackson.map.ObjectMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionnaireSettingImpl implements QuestionnaireSettingService {

    @Autowired
    private QuestionnaireMapper questionnaireMapper;






    private ObjectMapper objectMapper = new ObjectMapper();
    Agreement agreement =new Agreement();



//    @Override
//    public boolean setting(String formid, String dateTimeRange, int ceiling, Boolean repeatAnswer, String repeatTip, Boolean handling, Boolean shareWithTheQuery, String ipLimit, String weChatLimit, String phoneLimit) throws IOException, ParseException {
////        String [] time = dateTimeRange.split(",");
////        time[0] = time[0].replace("\"","");
////        time[1] = time[1].replace("\"","");
//        UpdateWrapper<Questionnaire> wrapper = new UpdateWrapper<>();
//        String[] limitType = {"ip_limit", "we_chat_limit", "phone_limit"};
//        String[] limitValue = {ipLimit, weChatLimit, phoneLimit};// 打死我都不用夺命连环if!
////        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        wrapper.eq("formid", formid);
////        wrapper.set("ceiling",ceiling).set("repeat_answer",repeatAnswer).set("handling", handling).set("share_with_the_query", shareWithTheQuery).set("repeat_tip",repeatTip).set("start_time",ft.parse(time[0])).set("end_time",ft.parse(time[1]));
//        wrapper.set("ceiling",ceiling).set("repeat_answer",repeatAnswer).set("handling", handling).set("share_with_the_query", shareWithTheQuery).set("repeat_tip",repeatTip).set("date_time_range",dateTimeRange);
//        for(int i = 0; i < 3; ++ i) {// 存:将三种限制类型打包存至表中
//            if (! limitValue[i].equals("null")){
//                Map<String, Object> maps = objectMapper.readValue(limitValue[i], new TypeReference<Map<String,Object>>() {});
//                StringBuilder temp  = new StringBuilder();
//                wrapper.set(limitType[i], temp.append(maps.get("celling")).append(',').append(maps.get("type")).toString());
//            }
//            else {
//                wrapper.set(limitType[i], null);
//            }
//            if(questionnaireMapper.update(null, wrapper) < 1){
//                throw new RuntimeException("更新失败");
//            }
//        }
//        return true;
//    }

    @Override
    public boolean setting(String formid, String dateTimeRange, int ceiling, Boolean repeatAnswer, String repeatTip, Boolean handling, Boolean shareWithTheQuery, Boolean isIp, String ipLimit, Boolean isWechat, String weChatLimit, Boolean isPhone, String phoneLimit, String beforeTheStartOfTheClues, String afterTheEndOfTheClues, String questionnaireLimitTip) {
        UpdateWrapper<Questionnaire> wrapper = new UpdateWrapper<>();
        wrapper.eq("formid", formid)
                .set("date_time_range", dateTimeRange)
                .set("ceiling",ceiling)
                .set("repeat_answer", repeatAnswer)
                .set("repeat_tip",repeatTip)
                .set("handling", handling)
                .set("share_with_the_query",shareWithTheQuery)
                .set("is_ip", isIp)
                .set("ip_limit",ipLimit)
                .set("is_wechat", isWechat)
                .set("we_chat_limit",weChatLimit)
                .set("is_phone", isPhone)
                .set("phone_limit",phoneLimit)
                .set("before_the_start_of_the_clues",beforeTheStartOfTheClues)
                .set("after_the_end_of_the_clues",afterTheEndOfTheClues)
                .set("questionnaire_limit_tip",questionnaireLimitTip);

        return questionnaireMapper.update(null, wrapper) == 1;
    }



    @Override
    public boolean handlingWayAfterSummit(String formid, int type, String content, Boolean handling) {
        UpdateWrapper<Questionnaire> wrapper = new UpdateWrapper<Questionnaire>();
        wrapper.eq("formid", formid).set("type", type).set("content", content).set("handling", handling ? 1 : 0);
        return questionnaireMapper.update(null, wrapper) == 1;
    }

    @Override
    public Questionnaire getHandlingWayAfterSummit(String formid) {

        return questionnaireMapper.getHandlingWayAfterSummit(formid);
    }

    @Override
    public boolean SetupSharing(String formid, String isTheTitle, String subtitle, Boolean isAllowedToShare,String imgUrl ,  Boolean shareWithTheQuery) {
        UpdateWrapper<Questionnaire> wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("formid", formid)
                .set("is_the_title",isTheTitle)
                .set("subtitle",subtitle)
                .set("is_allowed_to_share",isAllowedToShare)
                .set("img_url",imgUrl)
                .set("share_with_the_query",shareWithTheQuery);
        if(questionnaireMapper.update(null, wrapper1) < 1){
            throw new RuntimeException("更新失败");
        }
        return true;

    }






    @Override
    public Map<String, Object> GetSettingsSharing(String formid) {
//        System.out.println(questionnaireMapper.getSettingsSharing(formid)+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        return questionnaireMapper.getSettingsSharing(formid);
    }




    @Override
    public Questionnaire getSetting(String formid) {
//        return questionnaireMapper.getQuestionnaireSetting(formid);
        if(formid == null || formid.equals("") || formid.equals("undefined")){
            throw new RuntimeException("获取设置失败");
        }

        //2023.09.04 这里不应该受【问卷开始和结束时间】的约束，假如没有设置开始结束时间
        //2023.09.06 这里【问卷开始和结束时间】不可能存在单独没有设置开始结束时间，要么一起全部设置了，要么就是其他都没设置
        if(questionnaireMapper.getQuestionnaireSetting(formid).getDateTimeRange() != null){
            return questionnaireMapper.getQuestionnaireSetting(formid);
        }

        return null;

    }
}
